﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Vms.DB;
using Vms.Business;
using System.Text;
using System.IO;

public partial class NgoaiTe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DB m_db = new DB();
        Util m_util = new Util();
        WriteFile wf = new WriteFile();
        Util.TSession m_tSession = new Util.TSession();
        Util.TCharging m_tCharging = new Util.TCharging();
        try
        {
            int e_variable = 0;
            string d_variable = "";
            m_tSession.UserMSISDN = m_util.GetHeaderValue(Request.Headers, "User-MSISDN");
            m_tSession.User_ID = m_util.GetHeaderValue(Request.Headers, "User-UID");
            string str = "";
            int service_id = 0;
            str = "<?xml version='1.0' encoding='ISO-8859-1'?>" +
                   "<!DOCTYPE pages SYSTEM 'cellflash.dtd'>" +
                   "<pages descr='Samis'>" +
                   "<page> ";
            if (Request.Params["e"] != null)
            {
                e_variable = Int32.Parse(Request.Params["e"]);
            }
            if (Request.Params["desc"] != null)
            {
                d_variable = Request.Params["desc"];
            }
            if (Request.Params["sid"] != null)
            {
                service_id = Int32.Parse(Request.Params["sid"]);
            }
            else
            {
                service_id = Int32.Parse(Request.Params["cid"]);
                DataSet ds = new DataSet();
                ds = m_db.GetServiceByPage(service_id);
                DataTable dtb = new DataTable();
                dtb = ds.Tables[0].Clone();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    dtb.Rows.Add(ds.Tables[0].Rows[i].ItemArray);
                }
                foreach (DataRow dr in dtb.Rows)
                {
                    //str += "<a href='" + dr["URL"].ToString().Trim() + dr["Service_ID"].ToString() + "'>" + dr["Text"].ToString().Trim() + "(" + dr["Cost"].ToString() + ")</a><br/>";
                    str += "<a href='/850/sys/bill?a=" + dr["Cost"].ToString() + "&b=0&c=TiGia&t=0&re=0&i=000004:0000000041&d=" + dr["URL"].ToString().Trim() + dr["Service_ID"].ToString() + "'>" + dr["Text"].ToString().Trim() + "</a><br/>";
                }
                m_tSession.ServiceList = "4";
                m_tSession.KeyPress = "4";
                m_tSession.SessionID = "";
                m_tSession.IP = "";
                m_db.UpdateReQuestTbl(m_tSession);
            }
            if (service_id == 41)
            {
                //Gia Vang
                m_tSession.ServiceList = "41";
                m_tSession.KeyPress = "1";
                m_tSession.SessionID = "";
                m_tSession.IP = "";
                m_db.UpdateReQuestTbl(m_tSession);

                //Charging
                m_tCharging.ErrorCode = e_variable;
                m_tCharging.ErrorDescription = d_variable;
                m_tCharging.Service_ID = 41;
                m_tCharging.SessionID = Session.SessionID;
                m_tCharging.User_ID = m_util.GetHeaderValue(Request.Headers, "User-UID");
                m_tCharging.SessionID = Session.SessionID;
                m_tCharging.UserMSISDN = m_util.GetHeaderValue(Request.Headers, "User-MSISDN");
                m_tCharging.StartTime = DateTime.Now;
                if (e_variable == 0)
                {
                    m_tCharging.Cost = m_db.GetCostByService(m_tCharging.Service_ID);
                    m_tCharging.CDRIndicator = "DATA";
                    DataSet ds = m_db.GetGiaVang();
                    DataTable dtb = new DataTable();
                    dtb = ds.Tables[0].Clone();
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dtb.Rows.Add(ds.Tables[0].Rows[i].ItemArray);
                    }
                    str += DateTime.Now.ToString("dd/MM") + ":<br/>" +
                    "Loai Vang    Gia Mua    Gia Ban<br/>";
                    foreach (DataRow dr in dtb.Rows)
                    {
                        str += dr["LoaiVang"].ToString().Trim() + "    " + dr["Gia_Mua"].ToString().Trim() + "    " + dr["Gia_Ban"].ToString().Trim() + "<br/>";
                    }
                }
                else
                {
                    m_tCharging.Cost = 0;
                    m_util.write_log(Request.Url.OriginalString, 2);
                    m_util.WritelogHeader(Request.Headers);
                    m_tCharging.CDRIndicator = "WRONG";
                    str += "Tai khoan khong du de su dung dich vu. Xin vui long nap tien!";
                }
                m_db.InsertChargingTbl(m_tCharging);

            }
            else if (service_id == 42)
            {
                //Gia NT
                m_tSession.ServiceList = "42";
                m_tSession.KeyPress = "2";
                m_tSession.SessionID = "";
                m_tSession.IP = "";
                m_db.UpdateReQuestTbl(m_tSession);

                //Charging
                m_tCharging.ErrorCode = e_variable;
                m_tCharging.ErrorDescription = d_variable;
                m_tCharging.Service_ID = 42;
                m_tCharging.SessionID = Session.SessionID;
                m_tCharging.User_ID = m_util.GetHeaderValue(Request.Headers, "User-UID");
                m_tCharging.SessionID = Session.SessionID;
                m_tCharging.UserMSISDN = m_util.GetHeaderValue(Request.Headers, "User-MSISDN");
                m_tCharging.StartTime = DateTime.Now;
                if (e_variable == 0)
                {
                    m_tCharging.Cost = m_db.GetCostByService(m_tCharging.Service_ID);
                    m_tCharging.CDRIndicator = "DATA";
                    DataSet ds = m_db.GetGiaNT();
                    DataTable dtb = new DataTable();
                    dtb = ds.Tables[0].Clone();
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dtb.Rows.Add(ds.Tables[0].Rows[i].ItemArray);
                    }

                    str += DateTime.Now.ToString("dd/MM") + ":<br/>" +
                    "Loai NT    Gia Mua    Gia Ban<br/>";
                    foreach (DataRow dr in dtb.Rows)
                    {
                        str += dr["Loai_NT"].ToString().Trim() + "    " + dr["Gia_Mua"].ToString().Trim() + "    " + dr["Gia_Ban"].ToString().Trim() + "<br/>";
                    }
                }
                else
                {
                    m_tCharging.Cost = 0;
                    m_util.write_log(Request.Url.OriginalString, 2);
                    m_util.WritelogHeader(Request.Headers);
                    m_tCharging.CDRIndicator = "WRONG";
                    str += "Tai khoan khong du de su dung dich vu. Xin vui long nap tien!";
                }
                m_db.InsertChargingTbl(m_tCharging);

            }

            str += "</page>" +
                  "</pages>";
            Response.ClearHeaders();
            Response.AddHeader("content-type", "text/xml");
            Response.Write(str);
            wf.WriteStreamFile(str);
            Response.End();
        }
        catch (Exception ex)
        {
            wf.WriteError(ex.ToString());
        }
    }
}